package leetcode_1701_1800;

import java.util.Comparator;
import java.util.Deque;
import java.util.PriorityQueue;

public class LeeCode_1705 {
    public static void main(String[] args) {

    }
    private static int eatenApples(int[] apples, int[] days) {
        int n = apples.length, ans = 0;
        PriorityQueue<int[]> queue = new PriorityQueue<>(Comparator.comparingInt(o -> o[1]));
        for (int i = 0; i < n; i++) {
            while (!queue.isEmpty() && queue.peek()[1] < i){
                queue.poll();
            }
            if (!queue.isEmpty()){
                int[] p = queue.poll();
                p[0]--;
                ans++;
                if (p[0] > 0)
                    queue.add(p);
            }
            queue.add(new int[]{apples[i], days[i] + i});
        }
        int t = n;
        while (!queue.isEmpty()){
            while (!queue.isEmpty() && queue.peek()[1] < t){
                queue.poll();
            }
            if (!queue.isEmpty()){
                int[] p = queue.poll();
                p[0]--;
                ans++;
                if (p[0] > 0)
                    queue.add(p);
            }
            t++;
        }
        return ans;
    }
}